﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models.WebSite_HaiSheng;
using WebSite_HaiSheng;
using Utilities;
using System.IO;

namespace WebSite_HaiSheng.Controllers
{
    public class EmployeeRewardController : Controller
    {
        public ViewResult Index()
        {
            return View(CH.GetAllData<EmployeeReward>());
        }

        public ActionResult AjaxEmployeeRewardIndex()
        {
            return new DataJsonResult<EmployeeReward>() { Data = CH.GetAllData<EmployeeReward>() };
        }

        public ViewResult Details(int id)
        {
            return View(CH.GetDataById<EmployeeReward>(id));
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(EmployeeReward item, IEnumerable<HttpPostedFileBase> attachments)
        {
            if (attachments == null || attachments.Count() == 0)
            {
                ModelState.AddModelError("", "未发现上传文件");
            }
            if (ModelState.IsValid)
            {
                foreach (var file in attachments)
                {
                    var fileName = Path.GetFileName(file.FileName).Replace(" ", ""); ;
                    string serverpath = "/Uploads/Employee";
                    string path = Server.MapPath(serverpath);
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    var physicalPath = Path.Combine(path, fileName);
                    file.SaveAs(physicalPath);
                    item.FileUrl = serverpath + "/" + fileName;
                }

                CH.Create<EmployeeReward>(item);
                return RedirectToAction("Index");
            }
            return View(item);
        }
        public ActionResult Edit(int id)
        {
            return View(CH.GetDataById<EmployeeReward>(id));
        }

        [HttpPost]
        public ActionResult Edit(EmployeeReward item, IEnumerable<HttpPostedFileBase> attachments)
        {
            if (ModelState.IsValid)
            {
                if (attachments != null)
                {
                    foreach (var file in attachments)
                    {
                        var fileName = Path.GetFileName(file.FileName).Replace(" ", ""); ;
                        string serverpath = "/Uploads/Employee";
                        string path = Server.MapPath(serverpath);
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        var physicalPath = Path.Combine(path, fileName);
                        file.SaveAs(physicalPath);
                        item.FileUrl = serverpath + "/" + fileName;
                    }
                }
                CH.Edit<EmployeeReward>(item);
                return RedirectToAction("Index");
            }
            return View(item);
        }

        public ActionResult Delete(int id)
        {
            return View(CH.GetDataById<EmployeeReward>(id));
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            CH.Delete<EmployeeReward>(id);
            return RedirectToAction("Index");
        }
    }
}